package com.rzjm.api.util;

import org.apache.commons.codec.binary.Hex;
import sun.misc.BASE64Decoder;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Base64;

public class Base64Util {


    public static void main(String[] args) {
        String s = "AGT//25lAAQAFQEAAP8AAAALwdkxNTEwMzAxNjUNAQABAAAA/wAABsHZyrGztQ0CFQEAAP8AAAAcyOuzocqxvOQ6MjAyMy0xMi0yMCAxNToxMDozMQ0DAAEAAAD/AAAI0rvCt8uzt+cAAABvCg==";
        s = "AGT//29xAAAEABUBBQADAP8AAAAOwdnKsbO1wrNMNDNQMjENAQEBBQADAAD/AAAJMtChyrE1MbfWDQIBAQUAAwD/AAAAB8frvbs51KoNAwEBBQADAAD/AAAI0rvCt8a9sLIAChHCs0w0M1AyMcfrvbu30TnUqgDTLg==";
        s = "AGT//292AAAEABUBBQADAP8AAAAOwdnKsbO1wrNMNDNQMjENAQEBBQADAAD/AAAIM9ChyrE0t9YNAgEBBQADAP8AAAAKx+u9uzEwLjXUqg0DAQEFAAMAAP8AAAjSu8K3xr2wsgAKIMKzTDQzUDIxx+u9u7fRMTAuNdSqACcz";

//        s = "MDA2NEZGRkY2RTY1MDAwNDAwMTUwMTAwMDBGRjAwMDAwMDBCQzFEOTMxMzUzMTMwMzMzMDMxMzYzNTBEMDEwMDAxMDAwMDAwRkYwMDAwMDZDMUQ5Q0FCMUIzQjUwRDAyMTUwMTAwMDBGRjAwMDAwMDFDQzhFQkIzQTFDQUIxQkNFNDNBMzIzMDMyMzMyRDMxMzIyRDMyMzAyMDMxMzUzQTMxMzAzQTMzMzEwRDAzMDAwMTAwMDAwMEZGMDAwMDA4RDJCQkMyQjdDQkIzQjdFNzAwMDAwMDZGMEE=";
        //63682E636F6D17
        System.out.println("解析数据： " + base64ToHex(s));
//        s = "MDA2NEZGRkY2RjVDMDAwMDA0MDAxNTAxMDUwMDAzMDBGRjAwMDAwMDA2QjVEQUQyQkJEMEQwMEQwMTAxMDEwNTAwMDMwMDAwRkYwMDAwMDZCNURBQjZGRUQwRDAwRDAyMDEwMTA1MDAwMzAwRkYwMDAwMDAwNkI1REFDOEZERDBEMDBEMDMwMTAxMDUwMDAzMDAwMEZGMDAwMDA4QkJCNkQzQURCOUUyQzFEOTAwMEEwOEJCQjZEM0FEQjlFMkMxRDkwMDA2MzQ=";
//        System.out.println(base64ToHex(s));

//        byte[] bytes = Base64.getDecoder().decode(s);
//        String res = new String(bytes);
//        System.out.println("解密： " + res);

        s = "0064FFFF6F44000A0200150102000300FF00000008D4C14231323334350D01150102000300FF00000008BBB6D3ADB9E2C1D9000A11D4C14231323334352CBBB6D3ADB9E2C1D900100006A84C";
        s = "0064FFFF6E6500040015010000FF0000000BC1D93135313033303136350D010001000000FF000006C1D9CAB1B3B50D0215010000FF0000001CC8EBB3A1CAB1BCE43A323032332D31322D32302031353A31303A33310D030001000000FF000008D2BBC2B7CBB3B7E70000006F0A";
        s = "0064FFFF6F5C00000400150105000300FF00000006B5DAD2BBD0D00D0101010500030000FF000006B5DAB6FED0D00D02010105000300FF00000006B5DAC8FDD0D00D0301010500030000FF000008BBB6D3ADB9E2C1D9000A08BBB6D3ADB9E2C1D9000634";
        s = "0064FFFF6F7600000400150105000300FF0000000EC1D9CAB1B3B5C2B34C34335032310D0101010500030000FF00000833D0A1CAB134B7D60D02010105000300FF0000000AC7EBBDBB31302E35D4AA0D0301010500030000FF000008D2BBC2B7C6BDB0B2000A20C2B34C3433503231C7EBBDBBB7D131302E35D4AA002733";

//        s = "63682E636F6D17";
//        System.out.println(getBase64Encode(s));
//
//
//        // BASE64加密
////        BASE64Encoder encoder = new BASE64Encoder();
////        String data = encoder.encode(s.getBytes());
//        String ss = Base64.getEncoder().encodeToString(ByteUtil.hexStringToByteArray(s));
//        System.out.println("BASE64加密：" + ss);
//
//        ss = Base64Utils.encodeToUrlSafeString(s.getBytes(StandardCharsets.UTF_8));
//        System.out.println("BASE64加密：111   " + ss);

//        System.out.println("解密： " + base64ToHex(ss));

//        String str = "hello word";
        // JDK1.8 Base64加密
//        String encode = Base64.getEncoder().encodeToString(s.getBytes());
//        System.out.println("a  " + encode);

//        // JDK1.8 Base64解密
//        byte[] bytes = Base64.getDecoder().decode(ss);
//        String res = new String(bytes);
//        System.out.println(res);

    }

    // 加密
    public static String getBase64Encode(String str) {
        byte[] b = null;
        String s = null;
        try {
            b = str.getBytes("utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        if (b != null) {
//            s = new BASE64Encoder().encode(b);
            s = Base64.getEncoder().encodeToString(b);
        }
        return s;
    }

    // 解密
    public static String getBase64Decode(String s) {
        byte[] b = null;
        String result = null;
        if (s != null) {
            BASE64Decoder decoder = new BASE64Decoder();
            try {
                b = decoder.decodeBuffer(s);
                result = new String(b, "utf-8");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return result;
    }


    public static String getBase64ByImgPath(String imagePath){
        String ret = "";
        // 图片文件路径
//        String imagePath = "path/to/your/image.jpg";

        // 读取图片文件到字节数组
        Path path = new File(imagePath).toPath();
        try {
            byte[] imageBytes = Files.readAllBytes(path);

            // 对字节数组进行Base64编码
            ret = Base64.getEncoder().encodeToString(imageBytes);

            // 输出Base64编码后的字符串
            System.out.println(ret);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return ret;
    }


    /**
     * base64编码格式的16进制转16进制字符串
     * @param base64HexString  base64编码格式的16进制
     * @return 16进制字符串
     */
    public static String base64ToHex(String base64HexString) {
        try {
            // 解码
            byte[] decode = Base64.getDecoder().decode(base64HexString);
            // 返回 16进制字符编码
            return Hex.encodeHexString(decode).toUpperCase();
        } catch (RuntimeException e) {
            // 抛出自定义异常
            throw new RuntimeException("base64编码格式的16进制转16进制字符串错误，参数格式错误!");
        }
    }

    /**
     * 16进制字符串转Base64字符串
     * @param hexString
     * @return
     */
    public static String hexToBase64(String hexString) {
        try {
            // 16进制解码
            byte[] bytes = Hex.decodeHex(hexString);
            // 返回 编码成base64
            return Base64.getEncoder().encodeToString(bytes);
        } catch (Exception e) {
            // 抛出自定义异常
            throw new RuntimeException("16进制转base64编码错误, 参数格式错误!");
        }
    }

    /**
     *  base64字符串转换成图片
     * @param base64
     * @param path
     * @return
     */
    public static void generateImage(String base64, String path,String fileName) {
        // 解密
        try {
            String savePath = path;//"/**/imgtest/";​
            // 去掉base64前缀 data:image/jpeg;base64,
            base64 = base64.substring(base64.indexOf(",", 1) + 1);
            // 解密，解密的结果是一个byte数组
            Base64.Decoder decoder = Base64.getDecoder();
            byte[] imgbytes = decoder.decode(base64);
            for (int i = 0; i < imgbytes.length; ++i) {
                if (imgbytes[i] < 0) {
                    imgbytes[i] += 256;
                }
            }

            // 保存图片
            OutputStream out = new FileOutputStream(savePath.concat(fileName));
            out.write(imgbytes);
            out.flush();
            out.close();
            // 返回图片的相对路径 = 图片分类路径+图片名+图片后缀
//            return imgClassPath;
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
